<template>
  <div class="login-container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24" class="form-col">
        <el-form
          :model="loginForm"
          :rules="rules"
          ref="ruleFormRef"
          label-width="auto"
          class="login_form"
          style="max-width: 600px"
        >
          <h1>欢迎登录！</h1>
          <h2>**管理系统。</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model.trim="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              size="default"
              @click="login()"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import type { FormRules } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
// import { reqLogin } from '@/api/user'
import { loginFormData } from '@/api/user/type'
import { ElMessage } from 'element-plus'
import { SET_TOKEN } from '@/utils/token'
import { useRouter } from 'vue-router'
const $router = useRouter()

const rules = reactive<FormRules<loginFormData>>({
  username: [
    {
      required: true,
      message: '必填项不能为空',
      trigger: 'change',
    },
  ],
  password: [
    {
      required: true,
      message: '必填项不能为空',
      trigger: 'change',
    },
  ],
})

const loginForm: loginFormData = reactive({
  username: '',
  password: '',
})

const loading = ref(false)
const ruleFormRef = ref()

const login = async () => {
  await ruleFormRef.value.validate()
  loading.value = true
  try {
    // const res = await reqLogin(loginForm)
    // if (res.code !== 200) {
    //   ElMessage({
    //     type: 'error',
    //     message: res.data,
    //     duration: 1000,
    //   })
    //   loading.value = false
    //   return
    // }
    ElMessage({
      type: 'success',
      message: '登录成功！',
      duration: 1000,
    })
    // SET_TOKEN((res.data as string))
    SET_TOKEN('abcdefghijklmnopqrstuvwxyx1234567890')
    $router.push('/home')
    loading.value = false
  } catch (error) {
    console.log(error)
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background: url('../../assets/images/background.jpg') no-repeat;
  background-size: cover;

  .form-col {
    width: 100%;
    height: 100vh;
    display: flex;
    align-items: center;

    .el-form {
      width: 60%;
      background: url('@/assets/images/login_form.png') no-repeat;
      background-size: cover;
      padding: 40px;

      h1 {
        font-size: 40px;
        font-weight: 600;
        margin: 20px 0;
      }

      h2 {
        font-size: 20px;
        font-weight: 400;
        margin-bottom: 20px;
      }

      .el-button {
        width: 100%;
      }
    }
  }
}
</style>
